package com.hainatong.shiphelper.module.OAmgr.fragment;


import android.os.Bundle;
import android.view.View;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentOaShenpiApplyBinding;
import com.hainatong.shiphelper.mainhome.entity.MsgBean;
import com.hainatong.shiphelper.module.OAmgr.viewmodel.OAShenPiViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.loadsir.core.LoadService;
import com.hainatong.mvvmbase.loadsir.core.LoadSir;
import com.hainatong.mvvmbase.loadsir.layoutcallback.EmptyCallback;
import com.hainatong.mvvmbase.loadsir.layoutcallback.ErrorCallback;
import com.hainatong.mvvmbase.loadsir.layoutcallback.LoadingCallback;


public class OAShenPiApplyFragment extends MvvmLazyFragment<FragmentOaShenpiApplyBinding, OAShenPiViewModel>
{
    private static String OA_SHENPI_APPLY_TAG = "OAShenPiApplyFragment";

    private MsgBean applyMsg;

    private LoadService loadSer;

    public static OAShenPiApplyFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( OA_SHENPI_APPLY_TAG , typeCode );
        OAShenPiApplyFragment applyFragment = new OAShenPiApplyFragment();
        applyFragment.setArguments( bundle );
        return applyFragment;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(OA_SHENPI_APPLY_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            applyMsg = GsonUtil.json2Obj( type, MsgBean.class );
        }
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_oa_shenpi_apply;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {
    }

    @Override
    protected void initView(View view)
    {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
                .addCallback(new ErrorCallback())
                .build();
        //loadSer = loadSir.register(viewDataBinding.rootView,v -> { onRetryBtnClick(); });

    }

    @Override
    protected void lazyLoad()
    {
        if(ObjectUtils.isNotEmpty( applyMsg ))
        {
            //loadSer.showCallback( LoadingCallback.class );
            viewModel.queryVacationInfoById( applyMsg.getOa_table_id() );
        }
        // 提交的休假申请
        viewModel.getApplyVacationResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                //loadSer.showSuccess();
                if( result.getData() != null )
                {
                    viewDataBinding.setVacation( result.getData() );
                    viewDataBinding.executePendingBindings();
                }
            }
            else
            {
                //loadSer.showCallback( ErrorCallback.class );
                ToastUtils.showShort( result.getMsg() );
            }
        } );
    }
}
